<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>loader.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_module_loader.js.html">loader.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> *
<a name="line17"></a> * @fileoverview This class supports the dynamic loading of compiled
<a name="line18"></a> * javascript modules at runtime, as descibed in the designdoc.
<a name="line19"></a> *
<a name="line20"></a> *   &lt;http://go/js_modules_design&gt;
<a name="line21"></a> *
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.module.Loader&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line27"></a>goog.require(&#39;goog.array&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom&#39;);
<a name="line29"></a>goog.require(&#39;goog.object&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * The dynamic loading functionality is defined as a class. The class
<a name="line35"></a> * will be used as singleton. There is, however, a two step
<a name="line36"></a> * initialization procedure because parameters need to be passed to
<a name="line37"></a> * the goog.module.Loader instance.
<a name="line38"></a> *
<a name="line39"></a> * @constructor
<a name="line40"></a> */
<a name="line41"></a>goog.module.Loader = function() {
<a name="line42"></a>  /**
<a name="line43"></a>   * Map of module name/array of {symbol name, callback} pairs that are pending
<a name="line44"></a>   * to be loaded.
<a name="line45"></a>   * @type {Object}
<a name="line46"></a>   * @private
<a name="line47"></a>   */
<a name="line48"></a>  this.pending_ = {};
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * Provides associative access to each module and the symbols of each module
<a name="line52"></a>   * that have aready been loaded (one lookup for the module, another lookup
<a name="line53"></a>   * on the module for the symbol).
<a name="line54"></a>   * @type {Object}
<a name="line55"></a>   * @private
<a name="line56"></a>   */
<a name="line57"></a>  this.modules_ = {};
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * Map of module name to module url. Used to avoid fetching the same URL
<a name="line61"></a>   * twice by keeping track of in-flight URLs.
<a name="line62"></a>   * Note: this allows two modules to be bundled into the same file.
<a name="line63"></a>   * @type {Object}
<a name="line64"></a>   * @private
<a name="line65"></a>   */
<a name="line66"></a>  this.pendingModuleUrls_ = {};
<a name="line67"></a>
<a name="line68"></a>  /**
<a name="line69"></a>   * The base url to load modules from. This property will be set in init().
<a name="line70"></a>   * @type {?string}
<a name="line71"></a>   * @private
<a name="line72"></a>   */
<a name="line73"></a>  this.urlBase_ = null;
<a name="line74"></a>
<a name="line75"></a>  /**
<a name="line76"></a>   * Array of modules that have been requested before init() was called.
<a name="line77"></a>   * If require() is called before init() was called, the required
<a name="line78"></a>   * modules can obviously not yet be loaded, because their URL is
<a name="line79"></a>   * unknown. The modules that are requested before init() are
<a name="line80"></a>   * therefore stored in this array, and they are loaded at init()
<a name="line81"></a>   * time.
<a name="line82"></a>   * @type {Array.&lt;string&gt;}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.pendingBeforeInit_ = [];
<a name="line86"></a>};
<a name="line87"></a>goog.addSingletonGetter(goog.module.Loader);
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Creates a full URL to the compiled module code given a base URL and a
<a name="line92"></a> * module name. By default it&#39;s urlBase + &#39;_&#39; + module + &#39;.js&#39;.
<a name="line93"></a> * @param {string} urlBase URL to the module files.
<a name="line94"></a> * @param {string} module Module name.
<a name="line95"></a> * @return {string} The full url to the module binary.
<a name="line96"></a> * @private
<a name="line97"></a> */
<a name="line98"></a>goog.module.Loader.prototype.getModuleUrl_ = function(urlBase, module) {
<a name="line99"></a>  return urlBase + &#39;_&#39; + module + &#39;.js&#39;;
<a name="line100"></a>};
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * The globally exported name of the load callback. Matches the
<a name="line105"></a> * definition in the js_modular_binary() BUILD rule.
<a name="line106"></a> * @type {string}
<a name="line107"></a> */
<a name="line108"></a>goog.module.Loader.LOAD_CALLBACK = &#39;__gjsload__&#39;;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Loads the module by evaluating the javascript text in the current
<a name="line113"></a> * scope. Uncompiled, base identifiers are visible in the global scope;
<a name="line114"></a> * when compiled they are visible in the closure of the anonymous
<a name="line115"></a> * namespace. Notice that this cannot be replaced by the global eval,
<a name="line116"></a> * because the global eval isn&#39;t in the scope of the anonymous
<a name="line117"></a> * namespace function that the jscompiled code lives in.
<a name="line118"></a> *
<a name="line119"></a> * @param {string} t_ The javascript text to evaluate. IMPORTANT: The
<a name="line120"></a> *   name of the identifier is chosen so that it isn&#39;t compiled and
<a name="line121"></a> *   hence cannot shadow compiled identifiers in the surrounding scope.
<a name="line122"></a> * @private
<a name="line123"></a> */
<a name="line124"></a>goog.module.Loader.loaderEval_ = function(t_) {
<a name="line125"></a>  eval(t_);
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Initializes the Loader to be fully functional. Also executes load
<a name="line131"></a> * requests that were received before initialization. Must be called
<a name="line132"></a> * exactly once, with the URL of the base library. Module URLs are
<a name="line133"></a> * derived from the URL of the base library by inserting the module
<a name="line134"></a> * name, preceded by a period, before the .js prefix of the base URL.
<a name="line135"></a> *
<a name="line136"></a> * @param {string} baseUrl The URL of the base library.
<a name="line137"></a> * @param {Function=} opt_urlFunction Function that creates the URL for the
<a name="line138"></a> *     module file. It will be passed the base URL for module files and the
<a name="line139"></a> *     module name and should return the fully-formed URL to the module file to
<a name="line140"></a> *     load.
<a name="line141"></a> */
<a name="line142"></a>goog.module.Loader.prototype.init = function(baseUrl, opt_urlFunction) {
<a name="line143"></a>  // For the use by the module wrappers, loaderEval_ is exported to
<a name="line144"></a>  // the page. Note that, despite the name, this is not part of the
<a name="line145"></a>  // API, so it is here and not in api_app.js. Cf. BUILD. Note this is
<a name="line146"></a>  // done before the first load requests are sent.
<a name="line147"></a>  goog.exportSymbol(goog.module.Loader.LOAD_CALLBACK,
<a name="line148"></a>      goog.module.Loader.loaderEval_);
<a name="line149"></a>
<a name="line150"></a>  this.urlBase_ = baseUrl.replace(&#39;.js&#39;, &#39;&#39;);
<a name="line151"></a>  if (opt_urlFunction) {
<a name="line152"></a>    this.getModuleUrl_ = opt_urlFunction;
<a name="line153"></a>  }
<a name="line154"></a>
<a name="line155"></a>  goog.array.forEach(this.pendingBeforeInit_, function(module) {
<a name="line156"></a>    this.load_(module);
<a name="line157"></a>  }, this);
<a name="line158"></a>  goog.array.clear(this.pendingBeforeInit_);
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Requests the loading of a symbol from a module. When the module is
<a name="line164"></a> * loaded, the requested symbol will be passed as argument to the
<a name="line165"></a> * function callback.
<a name="line166"></a> *
<a name="line167"></a> * @param {string} module The name of the module. Usually, the value
<a name="line168"></a> *     is defined as a constant whose name starts with MOD_.
<a name="line169"></a> * @param {number|string} symbol The ID of the symbol. Usually, the value is
<a name="line170"></a> *     defined as a constant whose name starts with SYM_.
<a name="line171"></a> * @param {Function} callback This function will be called with the
<a name="line172"></a> *     resolved symbol as the argument once the module is loaded.
<a name="line173"></a> */
<a name="line174"></a>goog.module.Loader.prototype.require = function(module, symbol, callback) {
<a name="line175"></a>  var pending = this.pending_;
<a name="line176"></a>  var modules = this.modules_;
<a name="line177"></a>  if (modules[module]) {
<a name="line178"></a>    // already loaded
<a name="line179"></a>    callback(modules[module][symbol]);
<a name="line180"></a>  } else if (pending[module]) {
<a name="line181"></a>    // loading is pending from another require of the same module
<a name="line182"></a>    pending[module].push([symbol, callback]);
<a name="line183"></a>  } else {
<a name="line184"></a>    // not loaded, and not requested
<a name="line185"></a>    pending[module] = [[symbol, callback]];  // Yes, really [[ ]].
<a name="line186"></a>    // Defer loading to initialization if Loader is not yet
<a name="line187"></a>    // initialized, otherwise load the module.
<a name="line188"></a>    if (this.urlBase_) {
<a name="line189"></a>      this.load_(module);
<a name="line190"></a>    } else {
<a name="line191"></a>      this.pendingBeforeInit_.push(module);
<a name="line192"></a>    }
<a name="line193"></a>  }
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Registers a symbol in a loaded module. When called without symbol,
<a name="line199"></a> * registers the module to be fully loaded and executes all callbacks
<a name="line200"></a> * from pending require() callbacks for this module.
<a name="line201"></a> *
<a name="line202"></a> * @param {string} module The name of the module. Cf. parameter module
<a name="line203"></a> *     of method require().
<a name="line204"></a> * @param {number|string=} opt_symbol The symbol being defined, or nothing when
<a name="line205"></a> *     all symbols of the module are defined. Cf. parameter symbol of method
<a name="line206"></a> *     require().
<a name="line207"></a> * @param {Object=} opt_object The object bound to the symbol, or nothing when
<a name="line208"></a> *     all symbols of the module are defined.
<a name="line209"></a> */
<a name="line210"></a>goog.module.Loader.prototype.provide = function(
<a name="line211"></a>    module, opt_symbol, opt_object) {
<a name="line212"></a>  var modules = this.modules_;
<a name="line213"></a>  var pending = this.pending_;
<a name="line214"></a>  if (!modules[module]) {
<a name="line215"></a>    modules[module] = {};
<a name="line216"></a>  }
<a name="line217"></a>  if (opt_object) {
<a name="line218"></a>    // When an object is provided, just register it.
<a name="line219"></a>    modules[module][opt_symbol] = opt_object;
<a name="line220"></a>  } else if (pending[module]) {
<a name="line221"></a>    // When no object is provided, and there are pending require()
<a name="line222"></a>    // callbacks for this module, execute them.
<a name="line223"></a>    for (var i = 0; i &lt; pending[module].length; ++i) {
<a name="line224"></a>      var symbol = pending[module][i][0];
<a name="line225"></a>      var callback = pending[module][i][1];
<a name="line226"></a>      callback(modules[module][symbol]);
<a name="line227"></a>    }
<a name="line228"></a>    delete pending[module];
<a name="line229"></a>    delete this.pendingModuleUrls_[module];
<a name="line230"></a>  }
<a name="line231"></a>};
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Starts to load a module. Assumes that init() was called.
<a name="line236"></a> *
<a name="line237"></a> * @param {string} module The name of the module.
<a name="line238"></a> * @private
<a name="line239"></a> */
<a name="line240"></a>goog.module.Loader.prototype.load_ = function(module) {
<a name="line241"></a>  // NOTE(user): If the module request happens inside a click handler
<a name="line242"></a>  // (presumably inside any user event handler, but the onload event
<a name="line243"></a>  // handler is fine), IE will load the script but not execute
<a name="line244"></a>  // it. Thus we break out of the current flow of control before we do
<a name="line245"></a>  // the load. For the record, for IE it would have been enough to
<a name="line246"></a>  // just defer the assignment to src. Safari doesn&#39;t execute the
<a name="line247"></a>  // script if the assignment to src happens *after* the script
<a name="line248"></a>  // element is inserted into the DOM.
<a name="line249"></a>  goog.Timer.callOnce(function() {
<a name="line250"></a>    // The module might have been registered in the interim (if fetched as part
<a name="line251"></a>    // of another module fetch because they share the same url)
<a name="line252"></a>    if (this.modules_[module]) {
<a name="line253"></a>      return;
<a name="line254"></a>    }
<a name="line255"></a>
<a name="line256"></a>    var url = this.getModuleUrl_(this.urlBase_, module);
<a name="line257"></a>
<a name="line258"></a>    // Check if specified URL is already in flight
<a name="line259"></a>    var urlInFlight = goog.object.containsValue(this.pendingModuleUrls_, url);
<a name="line260"></a>    this.pendingModuleUrls_[module] = url;
<a name="line261"></a>    if (urlInFlight) {
<a name="line262"></a>      return;
<a name="line263"></a>    }
<a name="line264"></a>
<a name="line265"></a>    var s = goog.dom.createDom(&#39;script&#39;,
<a name="line266"></a>        {&#39;type&#39;: &#39;text/javascript&#39;, &#39;src&#39;: url});
<a name="line267"></a>    document.body.appendChild(s);
<a name="line268"></a>  }, 0, this);
<a name="line269"></a>};
</pre>


</body>
</html>
